import { Common } from "../../utils/common.js";
const ctx = wx.createCanvasContext('petCard')
const miniCodeImg = "/style/images/mini_code.jpg"
Page({
  data: {
    petName: "",
    cardBg: "",
    hasSave: false
  },

  onLoad: function (options) {
    let that = this
    console.log(options)
    if (options) {
      that.setData({
        petName: options.petName,
        cardBg: options.cardBg
      })
    }
  },
  saveImage(){
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success:res=>{
        wx.canvasToTempFilePath({
            canvasId: 'petCard',
            success: function (res) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  Common.myToast("已保存到手机相册", 2000);
                },
                fail: function (res) {
                }
              })
            },
            fail: function (res) {
                Common.myToast("请授权后保存", 2000);
            }
          })
      }
    })
},
  onReady: function () {
    let that = this

    ctx.drawImage(that.data.cardBg, 0, 0, wx.getSystemInfoSync().windowWidth * 0.68, 320)

    //fill first line text
    let firstLineText = "我是"+that.data.petName
    ctx.setFontSize(15)
    // ctx.setFillStyle("#ff8b5f")
    ctx.setFillStyle("#000000")
    ctx.setTextAlign('center')
    ctx.setTextBaseline('top')
    ctx.fillText(firstLineText, 60,  335)

    //fill second line text
    let secondLineText = "邀请你和我一起互动！";
    ctx.setFontSize(15)
    ctx.setFillStyle("#000000")
    ctx.setTextAlign('center')
    ctx.setTextBaseline('top')
    ctx.fillText(secondLineText, 85, 355)

    //fill third line text
    let thirdLineText = "长按二维码，友谊的小船起航咯~";
    ctx.setFontSize(12)
    ctx.setFillStyle("#000000")
    ctx.setTextAlign('center')
    ctx.setTextBaseline('top')
    ctx.fillText(thirdLineText, 90, 375)

    //mini code size
    ctx.drawImage(miniCodeImg, 195, 335, 60, 60)

    ctx.draw()
  }

})